package com.googlecode.montelin.server.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Id;

import com.google.appengine.api.datastore.QueryResultIterable;
import com.googlecode.montelin.server.dao.DAO;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Indexed;

@Entity
@Cached
public class Page extends AbstractEntity {

	@Id
	Long id;
	@Indexed
	String code;
	@Indexed
	String lang;
	@Embedded
	List<String> keywords;

	String menuCaption;
	String title;

	Integer position;
	
	Key<Page> parent;
	String description;
	
	String content;
	
	public Page() {
		
	}
	
	public Page(String code, String lang) {
		setCode(code);
		setLang(lang);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public List<String> getKeywords() {
		return keywords;
	}

	public void setKeywords(List<String> keywords) {
		this.keywords = keywords;
	}

	public String getMenuCaption() {
		return menuCaption;
	}

	public void setMenuCaption(String menuCaption) {
		this.menuCaption = menuCaption;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public Page getParent() {
		return DAO.getInstance().ofy().get(parent);
	}

	public void setParent(Page parent) {
		this.parent = new Key<Page>(Page.class, parent.id);
	}
	
	public List<Page> getChildren() {
		QueryResultIterable<Page> items = DAO.getInstance().ofy().query(Page.class).filter("parent",this).fetch();
		List<Page> out = new ArrayList<Page>();
		for (Page page : items) {
			out.add(page);
		}
		return out;
	}
	
	public void setDescription(String val) {
		this.description = val; 
	}

	public String getDescription() {
		return this.description;
	}
	
	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		if(content==null) return "TODO";
		return content;
	}

}
